package com.gary.service;

import cn.hutool.core.util.ObjectUtil;
import com.gary.bean.BackendUserPojo;
import com.gary.enums.DataPermissionVariableEnum;
import com.gary.util.ReflectUtil;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Notes
 * @Author gary
 * @Date 2021/10/14 11:26
 **/
public class DataPermissionServiceImpl {
    public Map<String, Object> getDataPermissionParam(String sessionId, String reqUri) throws Exception{
        /*logger.info("<======== 获取数据权限拼凑 SQL 参数  ============>");
        // 登陆用户信息
        BackendUserPojo backendUserPojo = userAuthority.getUserInfo(sessionId);
        // 用户对应的数据权限
        DataPermissionParamPojo dataPermissionParamPojo = backendUserPojo.getDataPermissionParamPojo();
        if(CommUtils.isNull(dataPermissionParamPojo)){
            return null;
        }
        // 返回值
        Map<String, Object> mapDataPermissionParam = new HashMap<>(3);
        // 不设置数据权限
        boolean isNotSetDataPermission = CollectionUtils.isNotEmpty(dataPermissionParamPojo.getDataPermissionNotSetUriList())
                && dataPermissionParamPojo.getDataPermissionNotSetUriList().contains(reqUri);
        mapDataPermissionParam.put(DataPermissionConstant.DATA_PERMISSION_NOT_SET_PERMISSION, isNotSetDataPermission);
        // 行权限参数
        Map<String, Map<String, List<QueryFilterRuleTree>>> dataPermissionRowParamMap = dataPermissionParamPojo.getDataPermissionRowParamMap();
        if(CommUtils.isNull(dataPermissionRowParamMap) || dataPermissionRowParamMap.size()<=0){
            return mapDataPermissionParam;
        }
        Map<String, List<QueryFilterRuleTree>> queryFilterRuleTreeMap = dataPermissionRowParamMap.get(reqUri);
        if(CommUtils.isNull(queryFilterRuleTreeMap) || queryFilterRuleTreeMap.size()<=0){
            return mapDataPermissionParam;
        }
        mapDataPermissionParam.put(DataPermissionConstant.DATA_PERMISSION_ROW, queryFilterRuleTreeMap);
        // 数据权限变量
        Map<String, String> variableMap = new HashMap<>(16);
        Map<String, Set<String>> dataPermissionVariableMap = dataPermissionParamPojo.getDataPermissionVariableMap();
        if(CommUtils.isNotNull(dataPermissionVariableMap) && dataPermissionVariableMap.size()>0){
            Set<String> dataPermissionVariableSet = dataPermissionVariableMap.get(reqUri);
            this.setDataPermissionVariable(dataPermissionVariableSet, variableMap, backendUserPojo);
        }
        mapDataPermissionParam.put(DataPermissionConstant.DATA_PERMISSION_ROW_VARIABLE, variableMap);

        return mapDataPermissionParam;*/
        return null;
    }

    /**
     * 设置数据权限变量
     *
     * @param dataPermissionVariableSet
     * @param variableMap
     * @param backendUserPojo
     * @throws Exception
     */
    public void setDataPermissionVariable(Set<String> dataPermissionVariableSet, Map<String, String> variableMap, BackendUserPojo backendUserPojo) throws Exception{
        if(CollectionUtils.isEmpty(dataPermissionVariableSet)){
            return;
        }
        if(ObjectUtil.isNull(variableMap)){
            variableMap = new HashMap<>(16);
        }

        for (String dataPermissionVariable : dataPermissionVariableSet) {
            DataPermissionVariableEnum dataPermissionVariableEnum = DataPermissionVariableEnum.get(dataPermissionVariable);
            if(ObjectUtil.isNull(dataPermissionVariableEnum)){
                continue;
            }
            // 反射执行方法获取变量的值
            String value = ReflectUtil.execMethod(dataPermissionVariableEnum.getMethodPath(), BackendUserPojo.class, backendUserPojo);
            variableMap.put(dataPermissionVariable, value);
        }
    }
}
